package com.eco.fanliapp.job.userjob;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.entity.SyBlacklist;
import com.eco.fanliapp.service.IAppUserService;
import com.eco.fanliapp.service.ISyBlacklistService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @date 2019-11-12
 * 签到超过三十次.未下单用户
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class SignNoOrderBlackTask implements Job, Serializable {


    private static final Logger log = LoggerFactory.getLogger(SignNoOrderBlackTask.class);
    @Autowired
    private IAppUserService iAppUserService;
    @Autowired
    private ISyBlacklistService iSyBlacklistService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public SignNoOrderBlackTask() {
    }

    @Override
    public void execute(JobExecutionContext context)  {

        //取得job详情
        JobDetail jobDetail = context.getJobDetail();

        JobDataMap dataMap =  jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }

        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        long startTime = System.currentTimeMillis();
        log.info("***************************************开始执行用户签到30次未下单用户拉入黑名单-SignNoOrderBlackTask：executeCount="+executeCount+"--startTime="+startTime);
        //搜索签到满30天 没有订单的用户，并设置为黑名单
        selectUserNotOrder();
        //洗白下单用户
        updateUserNotOrderBlack();
        long endTime = System.currentTimeMillis();
        log.info("***************************************开始执行用户签到30次未下单用户拉入黑名单-SignNoOrderBlackTask：executeCount=" + executeCount + "--endTime=" + endTime + "--耗时=" + (endTime - startTime));
    }

    /**
     * 查询未下单用户，签到大于30 ，加入黑名单
     */
    protected void selectUserNotOrder() {
        List<AppUser> list = iAppUserService.selectUserNotOrder();
        for (AppUser user:list
             ) {
            //查询该用户是否是黑名单
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id",user.getUserId());
            queryWrapper.eq("black_type",3);
            queryWrapper.eq("black_scope",1);
            SyBlacklist oneSyBlacklist = iSyBlacklistService.getOne(queryWrapper);
            if(oneSyBlacklist == null){
                //加入黑名单
                SyBlacklist syBlacklist = new SyBlacklist();
                syBlacklist.setUserId(user.getUserId());
                syBlacklist.setBlackType(3);//没有订单
                syBlacklist.setBlackReson("没有下订单");
                syBlacklist.setBlackScope(1);
                syBlacklist.setEnable(1);
                syBlacklist.setMeInviteCode(user.getMeInviteCode());
                syBlacklist.setDeleted(0);

                iSyBlacklistService.save(syBlacklist);
            }else {
                //已存在，洗白用户
                SyBlacklist syBlacklist = new SyBlacklist();
                syBlacklist.setTableId(oneSyBlacklist.getTableId());
                syBlacklist.setEnable(1);
                syBlacklist.setDeleted(0);
                iSyBlacklistService.updateById(syBlacklist);
            }
        }
    }

    /**
     * 洗白已下单用户
     */
    protected void updateUserNotOrderBlack() {
        List<Long> list = iAppUserService.updateUserNotOrderBlack();
        for (Long userId:list
        ) {
            //查询该用户是否是黑名单
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id",userId);
            queryWrapper.eq("black_type",3);
            queryWrapper.eq("black_scope",1);
            SyBlacklist oneSyBlacklist = iSyBlacklistService.getOne(queryWrapper);
            log.info("洗白用户",oneSyBlacklist.getUserId());
            if(oneSyBlacklist != null && oneSyBlacklist.getBlackScope() == 1 && oneSyBlacklist.getBlackType() == 3){

                //已存在，洗白用户
                SyBlacklist syBlacklist = new SyBlacklist();
                syBlacklist.setTableId(oneSyBlacklist.getTableId());
                syBlacklist.setEnable(0);
                syBlacklist.setDeleted(0);
                iSyBlacklistService.updateById(syBlacklist);
                log.info("洗白成功",oneSyBlacklist.getUserId());
            }

        }
    }
}
